package Client;

import java.io.IOException;
import java.io.InputStream;
import java.io.FileInputStream;
import java.io.File;
import java.net.MalformedURLException;

import javax.sound.midi.InvalidMidiDataException;
import javax.sound.midi.MidiSystem;
import javax.sound.midi.MidiUnavailableException;
import javax.sound.midi.Sequence;
import javax.sound.midi.Sequencer;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.UnsupportedAudioFileException;
//import javax.sound.*;
import javax.sound.sampled.*;
import javax.sound.sampled.DataLine.Info;

public class GameSoundManager {


	private final int EXTERNAL_BUFFER_SIZE = 524288; // 128Kb

	//private static final String BACKGROUND_MUSIC_PATH = "backgroundMusic.mid";

//	private static final String BACKGROUND_MUSIC_PATH = "Another_Race.mid";
	private static final String CANNON_FIRE_SOUND_PATH = "";
	private static final String SHIP_SPASH_SOUND_PATH = "splash.wav";

	//	private static File backGroundMusic;
	private static File cannonFireSound;
	private static File shipSplashSound;


	public GameSoundManager()throws Exception {
		//TODO add in back ground music
		//		backGroundMusic = new File (BACKGROUND_MUSIC_PATH);
		cannonFireSound = new File (CANNON_FIRE_SOUND_PATH);
		shipSplashSound = new File (SHIP_SPASH_SOUND_PATH);


	}

	/**
	 * Plays the correct sound
	 */
	public void playSounds(SoundTypez t) throws UnsupportedAudioFileException, IOException{


		try{

			if( t == SoundTypez.BACKGROUND_MUSIC){

				//playMidiLoop(backGroundMusic);

			} else if( t == SoundTypez.CANNON_BALL) {

				playWaveSound( new FileInputStream(cannonFireSound));

			} else if ( t == SoundTypez.SHIP_SPLASH){

				playWaveSound(  new FileInputStream(shipSplashSound) );

			}

		} catch(Exception e){

			System.err.println("Could not play sound");			
			e.printStackTrace();

		}


	}


	public void playWaveSound(InputStream musicWave) throws Exception{

		//setting up the playing		
		AudioInputStream playStream = AudioSystem.getAudioInputStream( musicWave);
		AudioFormat playFormat = playStream.getFormat();		
		Info playInfo =  new Info(SourceDataLine.class, playFormat);		
		SourceDataLine playSource = (SourceDataLine) AudioSystem.getLine(playInfo);
		playSource.open(playFormat, EXTERNAL_BUFFER_SIZE);
		playSource.start();


		//playing the music

		int readbytes = 0; 
		byte [] audioBuffer = new byte[EXTERNAL_BUFFER_SIZE];


		while ( readbytes != -1){


			readbytes = playStream.read(audioBuffer, 0, EXTERNAL_BUFFER_SIZE);

			if(readbytes >= 0){
				playSource.write(audioBuffer, 0, readbytes);
			}
		}  

	}

	public void playMidiLoop(File musicFile) throws Exception{

		try {
			// From file
			Sequence sequence = MidiSystem.getSequence(musicFile);        

			// Create a sequencer for the sequence
			Sequencer sequencer = MidiSystem.getSequencer();
			sequencer.open();
			sequencer.setSequence(sequence);

			// Start playing
			sequencer.start();
		} catch (MalformedURLException e) {
		} catch (IOException e) {
		} catch (MidiUnavailableException e) {
		} catch (InvalidMidiDataException e) {
		}


	}

	public static void main(String [] args){

		try {
			GameSoundManager my = new GameSoundManager();
			System.out.println("done");
			my.playSounds(SoundTypez.BACKGROUND_MUSIC);
		} catch (Exception e) {
			e.printStackTrace();
		}


	}


}
